package cn.wangxl.vertx.starter.web.session;

import io.vertx.core.*;
import io.vertx.core.http.CookieSameSite;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User;
import io.vertx.ext.web.Cookie;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.*;
import io.vertx.ext.web.sstore.ClusteredSessionStore;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;

import java.util.Date;

/**
 * body
 */
public class SessionTest extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {

    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();
    Router router = Router.router(vertx);

    // Create a clustered session store using defaults
    // 可以指定名称，指定超时时间
    SessionStore localSessionStore = LocalSessionStore.create(vertx);
//    SessionStore clusteredSessionStore = ClusteredSessionStore.create(vertx);

    SessionHandler sessionHandler = SessionHandler.create(localSessionStore);

    // the session handler controls the cookie used for the session
    // this includes configuring, for example, the same site policy
    // like this, for strict same site policy.
    sessionHandler.setCookieSameSite(CookieSameSite.STRICT);

    // Make sure all requests are routed through the session handler too
    router.route().handler(sessionHandler);


    // 认证授权(根据需求配置不同的认证方式)
    AuthProvider authProvider = new UserNameAndPasswordProvider();
    sessionHandler.setAuthProvider(authProvider);
    AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);


//    AuthHandler redirectAuthHandler = RedirectAuthHandler.create(authProvider);

// All requests to paths starting with '/private/' will be protected
    router.route("/info")
      .handler(basicAuthHandler);

    router.post("/login").handler(ctx->{
      ctx.request().setExpectMultipart(true);
      ctx.request().endHandler(end -> {

        String username = ctx.request().formAttributes().get("username");
        String password = ctx.request().formAttributes().get("password");

        System.out.println(username + "-->" + password);

        ctx.next();
      });
    }).handler(FormLoginHandler.create(authProvider))
      .handler(ctx -> {
        UserInfo userInfo = new UserInfo();
        userInfo.user.put("username","admin");
        userInfo.user.put("password","admin");
        ctx.setUser(userInfo);
        ctx.response().end("OK");
      });

    router.get("/info")
      .handler(ctx -> {
        Session session = ctx.session();
        ctx.response().end("OK");
      });


    server.requestHandler(router).listen(9999, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }


  public static void main(String[] args) throws Exception {
//    System.setProperty("vertx.logger-delegate-factory-class-name","io.vertx.core.logging.Log4j2LogDelegateFactory");
    System.setProperty("vertx.logger-delegate-factory-class-name","io.vertx.core.logging.Log4j2LogDelegateFactory");
    new SessionTest().start(Future.future());
  }
}
